$title	The TERM Model in MPSGE 

SET	user(*)	Industries and components of final demand
	c(*)	Commodities
	l(*)	Skills
	i(user)	Industries
	r(*)	Regions of use - production - origin
	s(*)	States;

$gdxin 'aggr.gdx'
$load user c=com i=ind l=occ r=regdst s=state

ALIAS (r,rr,rx,rm);

SET	fd(user)	Components of final demand /HOU,INV,GOV,EXP/,
	mrg(c)		Margins /trad, trns/,
	src		Sources /dom, imp/,
	dom(src)	Domestic market /dom/, 
	imp(src)	Import market /imp/;

PARAMETER
	cap(i,r)		"Rentals to capital (48x20)",
	lab(i,l,r)		"Wage matrix (48x9x20)",
	lnd(i,r)		"Rentals to land (48x20)",
	ptx(i,r)		"Taxes on production (48x20)",
	invest(c,i,r)		"investment at purchasers prices (48x48x20)",
	use(c,src,user,r)	"Basic + margin flows (48x2x52x20)",
	dep(i)			"Depreciation rates adapted from MONASH model (48)",
	expelas(c)		"Export demand elasticity (48) (+)",
	feed0(i)		"FEED0(i (48))",
	make(c,i,r)		"MAKE matrix (48x48x20)",
        suppmar(c,r,r,r)	"Margins supplied by REGPRD on goods passing from (2x20x20x20)",

	armsigma(c)		"Armington from PDA (Jan 2002),remapped (48)",
	frisch(r)		"Frisch LES parameter= - (total/luxury (20))",
	sigma1prim(i)		"CES substitution, primary factors (48)",
	pop(r)			"Regional population June 2001 (20)",
	regstate(r,s)		"Maps regions to states (20x8)",
	sigmadomdom(c)		"Between-region Armington (48)",
	sigma1lab(i)		"CES substitution between skill types (48)",
	sigmamar(mrg)		"Elasticity of substitution between regions of margin production (2)",

	stocks(i,r)		"Domestic inventories (48x20)",

	trade(c,src,r,r)	"Sourcing matrix (48x2x20x20)",
	tradmar(c,src,mrg,r,r)	"Margins on trade matrix (48x2x2x20x20)",
	taxes(c,src,user,r)	"Taxes  (48x2x52x20))",

	usewater(i,r)		"Value of water used (48x20)",
	wateruse(i,r)		"Water used (16x12)",
	wat_elas(i)		"Water-saving responsiveness (48)",
	wrights(i,r)		"Water rights (16x12)";

$load cap=v1cap lab=v1lab lnd=v1lnd ptx=v1ptx invest use=basmar dep expelas feed0 make suppmar armsigma 
$load frisch sigma1prim pop regstate sigmadomdom sigma1lab sigmamar stocks 
$load tradmar trade taxes usewater wateruse=waterirg wat_elas wrights=waterirga 

parameter	vtot(i,r)	Total output;
vtot(i,r) = sum((c,src), use(c,src,i,r) + taxes(c,src,i,r)) 
		+ usewater(i,r)
		+ sum(l,lab(i,l,r)) 
		+ cap(i,r) 
		+ lnd(i,r) 
		+ ptx(i,r);

parameter	imbalance(i,r)	Zero profit imbalances (% deviation);
imbalance(i,r)$round(vtot(i,r),5) = round(100 * (vtot(i,r)-sum(c, make(c,i,r))-stocks(i,r))/vtot(i,r), 2);
display imbalance;

parameter	marbal(mrg,rx,rm)	Margin balance;
marbal(mrg,rx,rm) = round(sum(r, suppmar(mrg,rx,rm,r)) - sum((c,src), tradmar(c,src,mrg,rx,rm)),3);
display marbal;

parameter	demand(c,src,r)		Demand,
		supply(c,src,r)		Supply
		usebalance(c,src,r)	Balance of trade flows and commodity use
		makebalance(c,r)	Balance of make and trade flows;

demand(c,src,r) = sum(user, use(c,src,user,r));
supply(c,src,r) = sum(rr, trade(c,src,rr,r) + sum(mrg, tradmar(c,src,mrg,rr,r)));

usebalance(c,src,r)$sum(user, use(c,src,user,r)) = round(100 * ( sum(user, use(c,src,user,r))/
				sum(rr, trade(c,src,rr,r) + sum(mrg, tradmar(c,src,mrg,rr,r)))-1),2);
display usebalance;

makebalance(c,r) = round(sum(i, make(c,i,r)) - sum((rx,rm), suppmar(c,rx,rm,r)) - sum(rr, trade(c,"dom",r,rr)),3);
display makebalance;

parameter	c0(r)			Aggregate final consumption
		ta(c,src,user,r)	Tax rate on aggregate goods
		pa0(c,src,user,r)	Reference price
		i0(r)			Base year investment
		ty(i,r)			Industry tax
		g0			Government demand
		supply(c,src,r)		Aggregate supply
		demdmar(mrg,rx,rm)	Margins demand
		bopdef			Current account deficit
		trnsf(r)		Net transfers to region r
		w0(r)			Base year water supply and demand
		e0(c,r)			Exports
		pe0(c,r)		Reference price of exports
		te(c,r)			Export tax rate
		vx0			Value of exports;

ta(c,src,user,r)$taxes(c,src,user,r) = taxes(c,src,user,r)/use(c,src,user,r);
pa0(c,src,user,r) = 1 + ta(c,src,user,r);
c0(r) = sum((c,src), use(c,src,"hou",r)+taxes(c,src,"hou",r));
i0(r) = sum((c,src), use(c,src,"inv",r)+taxes(c,src,"inv",r));
g0 = sum((c,src,r),use(c,src,"gov",r)+taxes(c,src,"gov",r));
supply(c,src,r) = sum(rr, trade(c,src,rr,r)+sum(mrg,tradmar(c,src,mrg,rr,r)));
demdmar(mrg,rx,rm) = sum(r, suppmar(mrg,rx,rm,r));
ty(i,r)$ptx(i,r) = ptx(i,r) / vtot(i,r);
trnsf(r) = c0(r) + sum(i, stocks(i,r)) + i0(r)
		- sum(i, sum(l, lab(i,l,r)) + lnd(i,r) + cap(i,r) + usewater(i,r));
w0(r) = sum(i, usewater(i,r));
e0(c,r) = use(c,"dom","exp",r);
te(c,r)$taxes(c,"dom","exp",r) = taxes(c,"dom","exp",r)/use(c,"dom","exp",r);
pe0(c,r) = 1 + te(c,r);
vx0 = sum((c,r), pe0(c,r)*e0(c,r));

bopdef = sum((c,rx,rm), trade(c,"imp",rx,rm)) - vx0;


parameter y0, ys0;
y0(i,r) = round(vtot(i,r) - stocks(i,r), 6);
ys0(c,r) = round(sum(i,make(c,i,r)),6);

parameter	trdbal		Components of regional trade balances,
		gdpele(*,*)	Components of GDP,
		gdpincsum(r,*)	Categories of income in GDP
		gdpexpsum(r,*)	GDP expenditure summary;

set	tbalcol		/imports,rexports,rimports,exports,netmarg/,
	gdpinccat	/land,labour,capital,prodtax,comtax,water/,
	gdpexpcat	/hou, inv, gov, stocks, exp, imports, rexports, rimports, netmar/;


*	Begin by computing trade balances for each of the states and for the 
*	country as a whole:

trdbal(r,"imports") = -sum((c,rr), trade(c,"imp",r,rr));
trdbal(r,"RExports") = sum((c,src,rr), trade(c,src,r,rr)) - sum((c,src),trade(c,src,r,r));
trdbal(r,"RImports") = -(sum((c,src,rr), trade(c,src,rr,r)) - sum((c,src),trade(c,src,r,r)));
trdbal(r,"Exports") = sum((c,src), use(c,src,"exp",r) + taxes(c,src,"exp",r)); 
trdbal(r,"NetMarg") = sum((mrg,rx,rm), suppmar(mrg,rx,rm,r)) - sum((mrg,rx,rr), suppmar(mrg,rx,r,rr));
trdbal(r,"balance") = sum(tbalcol, trdbal(r,tbalcol));
trdbal("balance",tbalcol) = sum(r, trdbal(r,tbalcol));
trdbal("balance","balance") = sum(tbalcol, sum(r, trdbal(r,tbalcol)));
trdbal("balance","check") = trdbal("balance","balance") + bopdef + eps;
display trdbal;


*	Check GDP accounting from the income and expenditure side:

*	Budget balance in the model is

*		gdpele(r,"C") + gdpele(r,"I") =  gdpele(r,"M") + gdpele(r,"T") 

gdpele(r,"M") = sum(i, lnd(i,r) + sum(l, lab(i,l,r)) + cap(i,r) + usewater(i,r));
gdpele(r,"R")   = sum(i, ptx(i,r)) + sum((c,src,user), taxes(c,src,user,r));
gdpele(r,"T") = trnsf(r);
gdpele(r,"C") = sum((c,src), use(c,src,"hou",r) + taxes(c,src,"hou",r));
gdpele(r,"I") = sum((c,src), use(c,src,"inv",r) + taxes(c,src,"inv",r)) + sum(i, stocks(i,r));
gdpele(r,"G") = sum((c,src), use(c,src,"gov",r) + taxes(c,src,"gov",r));
gdpele(r,"B") = -trdbal(r,"balance");
gdpele(r,"check") = gdpele(r,"M") + gdpele(r,"R") -
		    (gdpele(r,"C") + gdpele(r,"I") + gdpele(r,"G") - gdpele(r,"B"));
gdpele("GOVT","check") = sum(r, gdpele(r,"G") + gdpele(r,"T") - gdpele(r,"B") - gdpele(r,"R"));
display gdpele;

gdpincsum(r,"Land")    = sum(i, lnd(i,r));
gdpincsum(r,"Labour")  = sum(i, sum(l, lab(i,l,r)));
gdpincsum(r,"Capital") = sum(i, cap(i,r));
gdpincsum(r,"Water")   = sum(i, usewater(i,r));
gdpincsum(r,"ProdTax") = sum(i, ptx(i,r));
gdpincsum(r,"ComTax")  = sum((c,src,user), taxes(c,src,user,r));
gdpincsum(r,"total")   = sum(gdpinccat,gdpincsum(r,gdpinccat));
display gdpincsum;

*	Imports from ROW:

gdpexpsum(r,fd)         = sum((c,src), use(c,src,fd,r) + taxes(c,src,fd,r));
gdpexpsum(r,"Stocks")   = sum(i, stocks(i,r));
gdpexpsum(r,"Imports")  = -sum((c,rr), trade(c,"imp",r,rr));
gdpexpsum(r,"RExports") = sum((c,src,rr), trade(c,src,r,rr)) - sum((c,src),trade(c,src,r,r));
gdpexpsum(r,"RImports") = -(sum((c,src,rr), trade(c,src,rr,r)) - sum((c,src),trade(c,src,r,r)));
gdpexpsum(r,"NetMar")   = sum((mrg,rx,rm), suppmar(mrg,rx,rm,r)) - sum((mrg,rx,rr), suppmar(mrg,rx,r,rr));
gdpexpsum(r,"total")    = sum(gdpexpcat,gdpexpsum(r,gdpexpcat));
display gdpexpsum;

parameter	tarate	Tax rates (percentage)
		tyrate	Industrial tax rates (percentage);

tarate(r,src,c,fd)   = round( 100 * ta(c,src,fd,r), 1);
tarate(r,src,c,"id")$sum(i,use(c,src,i,r))
	= round( 100 * sum(i,ta(c,src,i,r)*use(c,src,i,r))/sum(i,use(c,src,i,r)), 1);
tyrate(r,i) = round(100 * ty(i,r));
option tarate:1:1:1;
display tarate;
option tyrate:0;
display tyrate;

$ontext
$model: term

$sectors:
	hc(r)				! Household consumption
	gov				! Government demand
	y(i,r)$vtot(i,r)		! Industrial activity
	ys(i,r)$y0(i,r)			! Commodity supply (make)
	m(c,src,r)$supply(c,src,r)	! Bilateral imports
	inv(r)				! Investment demand
	mrgd(mrg,rx,rm)$demdmar(mrg,rx,rm)	! Margin demand

$commodities:
	pc(r)				! Final consumption
	pinv(r)				! Invement cost
	pgov				! Public expenditure
	pm(c,src,r)$supply(c,src,r)	! Import price index
	p(c,r)$ys0(c,r)			! Output price
	pfx				! Price index of foreign exchange
	pmrg(mrg,rr,r)$demdmar(mrg,rr,r)! Margin price
	py(i,r)$y0(i,r)			! Industry output price
	pwater(r)$w0(r)			! Price of water 
	pcap(r)				! Price of capital
	plnd(r)				! Price of land
	plab(l,r)			! Price of labor

$consumers:
	ra(r)	! Representative agent
	govt	! Government

$auxiliary:
	e(c,r)$e0(c,r)	! Export demand
	vx		! Aggregate value of exports

*	Components of final demand currently treated as Cobb-Douglas aggregates:

$prod:hc(r)   s:1  c.tl:2
	o:pc(r)		q:c0(r)
	i:pm(c,src,r)	q:use(c,src,"hou",r)  a:govt  t:ta(c,src,"hou",r)  p:pa0(c,src,"hou",r)  c.tl:

*	Final investment demand is Leontief:

$prod:inv(r)  s:2  c.tl:4
	o:pinv(r)	q:i0(r)
	i:pm(c,src,r)	q:use(c,src,"inv",r)  a:govt  t:ta(c,src,"inv",r)  p:pa0(c,src,"inv",r) c.tl:

*	Government demand is also Leontief:

$prod:gov
	o:pgov		q:g0
	i:pm(c,src,r)	q:use(c,src,"gov",r)	a:govt t:ta(c,src,"gov",r) p:pa0(c,src,"gov",r)


*	Bilateral trade flows in domestic and imported goods, including margin
*	demand:

$prod:m(c,src,r)$supply(c,src,r) s:4  rr.tl:0
	o:pm(c,src,r)		q:supply(c,src,r)
	i:p(c,rr)$dom(src)	q:trade(c,src,rr,r)		rr.tl:
	i:pfx#(rr)$imp(src)	q:trade(c,src,rr,r)		rr.tl:
	i:pmrg(mrg,rr,r)	q:tradmar(c,src,mrg,rr,r)	rr.tl:

*	Margins are composed of service inputs from multiple regions:

$prod:mrgd(mrg,rx,rm)$demdmar(mrg,rx,rm)  s:sigmamar(mrg)
	o:pmrg(mrg,rx,rm)	q:demdmar(mrg,rx,rm)
	i:p(mrg,r)		q:suppmar(mrg,rx,rm,r)

*	Production combines water, intermediate inputs, and primary factors
*	to produce an industrial output index commodity:

$prod:y(i,r)$vtot(i,r)  s:0.5  mva:0  m(mva):0.5 c.tl(m):2  va(mva):sigma1prim(i)  ld(va):sigma1lab(i)
	o:py(i,r)	q:vtot(i,r)	a:govt	t:ty(i,r)
	i:pwater(r)	q:usewater(i,r)
	i:pm(c,src,r)	q:use(c,src,i,r)	a:govt  t:ta(c,src,i,r)  p:pa0(c,src,i,r)  c.tl:
	i:pcap(r)	q:cap(i,r)	va:
	i:plnd(r)	q:lnd(i,r)	va:
	i:plab(l,r)	q:lab(i,l,r)	ld:

*	Supply by commodity is determined through joint production in multiple sectors:

$prod:ys(i,r)$y0(i,r)  t:0
	o:p(c,r)	q:make(c,i,r)
	i:py(i,r)	q:y0(i,r)

*	Final demand in region r:

$demand:ra(r)
	e:plab(l,r)	q:(sum(i,lab(i,l,r)))
	e:plnd(r)	q:(sum(i,lnd(i,r)))
	e:pcap(r)	q:(sum(i,cap(i,r)))
	e:pwater(r)	q:w0(r)

	e:pfx		q:trnsf(r)
	e:py(i,r)	q:(-stocks(i,r))
	e:pinv(r)	q:(-i0(r))
	d:pc(r)	

$demand:govt
	e:pfx		q:(bopdef-sum(r,trnsf(r)))
	e:pm(c,"dom",r)	q:(-e0(c,r))		r:e(c,r)
	e:pfx		q:vx0			r:vx
	d:pgov

$constraint:e(c,r)$e0(c,r)
	e(c,r) =e= (pfx*pe0(c,r)/(pm(c,"dom",r)*(1+te(c,r))))**expelas(c);

$constraint:vx
	pfx * vx0 * vx  =e= sum((c,r)$e0(c,r), pm(c,"dom",r)*(1+te(c,r))*e0(c,r)*e(c,r));

$offtext
$sysinclude mpsgeset term

e.l(c,r)$e0(c,r) = 1;
vx.l = 1;

term.workspace = 50;
term.iterlim = 0;
$include term.gen
solve term using mcp;

term.iterlim = 10000;
$include term.gen
solve term using mcp;

